//
//  FindViewController.m
//  JZMCFM
//
//  Created by Mc on 15/10/30.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "FindViewController.h"
#import "HomeLabel.h"
@interface FindViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *titleScroll;
@property (strong, nonatomic) IBOutlet UIScrollView *contentScroll;
@property(nonatomic,strong)UIView *view;
@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.contentScroll.delegate = self;
    // 添加子控制器
    [self setupChildVcs];
    
    [self setupTitles];
    
    // 添加默认控制器
    UIViewController *firstVc = [self.childViewControllers firstObject];
    firstVc.view.frame = self.contentScroll.bounds;
    [self.contentScroll addSubview:firstVc.view];
    HomeLabel *firstLabel  = [self.titleScroll.subviews firstObject];
    firstLabel.scale = 1.0;
    
    //设置内容size
    CGFloat contentSContentW = self.childViewControllers.count * [UIScreen mainScreen].bounds.size.width;
    self.contentScroll.contentSize = CGSizeMake(contentSContentW, 0);
    
    UIImage *Image = [UIImage imageNamed:@"iconfont-iconfontfaxianweixuanzhong"];
    Image = [Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    UIImage *selectedImage = [UIImage imageNamed:@"iconfont-iconfontfaxianweixuanzhong副本"];
    [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:Image selectedImage:selectedImage];
}

- (void)setupChildVcs
{
    RecommendViewController *vc1 = [[RecommendViewController alloc] init];
    vc1.title = @"推荐";
    [self addChildViewController:vc1];
    
    ClassifyViewController *vc2 = [[ClassifyViewController alloc] init];
    vc2.title = @"分类";
    [self addChildViewController:vc2];
    
    ListViewController *vc3 = [[ListViewController alloc] init];
    vc3.title = @"榜单";
    [self addChildViewController:vc3];
    
    LiveViewController *vc4 = [[LiveViewController alloc] init];
    vc4.title = @"直播";
    [self addChildViewController:vc4];
    
    CompereViewController *vc5 = [[CompereViewController alloc] init];
    vc5.title = @"主播";
    [self addChildViewController:vc5];
    
}

- (void)setupTitles
{
    NSInteger count = self.childViewControllers.count;
    CGFloat labelW = 80;
    CGFloat labelH = 30;
    CGFloat labelY = 0;
    for (NSInteger i = 0; i < count; i++) {
        HomeLabel *label = [[HomeLabel alloc] init];
        label.tag = i;
        [self.titleScroll addSubview:label];
        
        // 设置frame
        CGFloat labelX = i * labelW;
        label.frame = CGRectMake(labelX, labelY, labelW, labelH);
        
        // 设置文字
        UIViewController *vc = self.childViewControllers[i];
        label.text = vc.title;
        
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelAction:)]];
    }
    
    // 设置scrollView的内容大小
    CGFloat titleContentW = count *labelW;
    self.titleScroll.contentSize = CGSizeMake(titleContentW, 0);
}
- (void)labelAction:(UITapGestureRecognizer *)sender
{
    // 获取被点击的label
    HomeLabel *label = (HomeLabel *)sender.view;
    // 计算x方向上的label
    CGFloat offsetX = label.tag * self.contentScroll.frame.size.width;
    
    // 设置偏移量
    CGPoint offset = CGPointMake(offsetX, self.contentScroll.contentOffset.y);
    [self.contentScroll setContentOffset:offset animated:YES];
}

#pragma mark - <UIScrollViewDelegate>
/**
 *  在scrollView动画结束时调用(添加子控制器的view到self.contentsScrollView)
 *  self.contentsScrollView == scrollView
 *  用户手动触发的动画结束，不会调用这个方法
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 获得当前需要显示的子控制器的索引
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 滚动标题栏（self.titlesScrollView）
    HomeLabel *label = self.titleScroll.subviews[index];
    CGFloat titlesW = self.titleScroll.frame.size.width;
    CGFloat offsetX = label.center.x - titlesW * 0.5;
    // 最大的偏移量
    CGFloat maxOffsetX = self.titleScroll.contentSize.width - titlesW;
    if (offsetX < 0) {
        offsetX = 0;
    } else if (offsetX > maxOffsetX) {
        offsetX = maxOffsetX;
    }
    CGPoint offset = CGPointMake(offsetX, self.titleScroll.contentOffset.y);
    [self.titleScroll setContentOffset:offset animated:YES];
    
    // 让其它label
        [self.titleScroll.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if (idx != index) {
                HomeLabel *otherLabel = self.titleScroll.subviews[idx];
                otherLabel.scale = 0.0;
            }
        }];
    
    UIViewController *vc = self.childViewControllers[index];
    // 如果子控制器的view已经在上面，就直接返回
    if (vc.view.superview) return;
    
    // 添加
    CGFloat vcW = scrollView.frame.size.width;
    CGFloat vcH = scrollView.frame.size.height;
    CGFloat vcX = index * vcW;
    CGFloat vcY = 0;
    vc.view.frame = CGRectMake(vcX, vcY, vcW, vcH);
    
    [scrollView addSubview:vc.view];
}

/**
 *  当scrollView停止滚动时调用这个方法(用手触发的动画停止,会调用这个方法)
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 1.需要进行控制的文字（2个文字）
    // 2.2个文字各自的比例值
    
    // 偏移量 / 宽度
#warning 这里最好取绝对值（保证计算出来的比例是非负数）
    CGFloat value = ABS(scrollView.contentOffset.x / scrollView.frame.size.width);
    
    // 左边文字的索引
    NSInteger leftIndex = (NSInteger)(scrollView.contentOffset.x / scrollView.frame.size.width);
    // 右边文字的索引
    NSInteger rightIndex = leftIndex + 1;
    
    // 右边文字的比例
    CGFloat rightScale = value - leftIndex;
    // 左边文字的比例
    CGFloat leftScale = 1 - rightScale;
    
    // 取出label设置大小和颜色
    HomeLabel *leftLabel = self.titleScroll.subviews[leftIndex];
    leftLabel.scale = leftScale;
        if (rightIndex < self.titleScroll.subviews.count) {
            HomeLabel *rightLabel = self.titleScroll.subviews[rightIndex];
            rightLabel.scale = rightScale;
        }
    
    
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
